
const key = 'WMUBZ-ND26V-RYDPC-UZPNY-CYQRV-GBB6L'; //使用在腾讯位置服务申请的key
const referer = '獭獭同城'; //调用插件的app的名称

// const category = '提货点';

function getAddress(locations = {}){
    const loc = locations;
    if(loc.hasOwnProperty("latitude") && loc.latitude != 0){
        const location = JSON.stringify(locations);
        wx.navigateTo({
            url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location 
        });
    }else{
        wx.getLocation({
            type: 'wgs84',
            success (res) {
                const latitude = res.latitude
                const longitude = res.longitude
                const speed = res.speed
                const accuracy = res.accuracy
                const location = JSON.stringify({
                    latitude,
                    longitude
                });
                wx.navigateTo({
                    url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location 
                });
            }
        })
    }
}
function getSetting(locations){
    wx.getSetting({
        success(result){
            if (result.authSetting['scope.userLocation']) {
                getAddress(locations);
            }else{
                wx.openSetting({
                    success(res) {
                        // 2.1 如果二次授权允许了 userLocation 权限， 就再次执行获取位置的接口
                        if (res.authSetting["scope.userLocation"]) {
                            getAddress(locations);
                        }
                    }
                })
    
            }
        }
    })
}
module.exports = {
    getAddress,
    getSetting,
};
